import 'dart:io';

import 'package:amway_superapp_creator_plugin/config/enum_config.dart';
import 'package:amway_superapp_creator_plugin/dialog/comm_tip_dialog.dart';
import 'package:amway_superapp_creator_plugin/routers/application.dart';
import 'package:amway_superapp_creator_plugin/utils/toast_util.dart';
import 'package:amway_superapp_creator_plugin/views/image_bean.dart';
import 'package:amway_superapp_creator_plugin/widget/asperct_raioimage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class MultimeadiaPreview extends StatefulWidget {
  List<ImageBean> images = [];
  int index;

  MultimeadiaPreview({this.images, this.index});

  @override
  _MultimeadiaPreviewState createState() => _MultimeadiaPreviewState();
}

class _MultimeadiaPreviewState extends State<MultimeadiaPreview> {
  void removeData(int index) {
    setState(() {
      widget.images.removeAt(index);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
          leading: leadingwidget(context),
          title: title(),
          centerTitle: true,
          actions: [
            delButton(context),
          ],
        ),
        body: WillPopScope(
          onWillPop: () {
            Application.router.pop(context, widget.images);
            return;
          },
          child: _builderBody(context),
        ));
  }

  Widget title() {
    return Text(
      "${widget.index + 1}/${widget.images.length}",
      style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(19)),
    );
  }

  Widget _builderBody(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colors.black,
      child: Swiper(
        key: GlobalKey(),
        loop: false,
        autoplay: false,
        itemBuilder: (BuildContext context, int index) {
          return buiderImageItem(context, index);
        },
        index: widget.index,
        onIndexChanged: (index) {
          setState(() {
            widget.index = index;
          });
        },
        itemCount: widget.images.length,
      ),
    );
  }

  Widget buiderImageItem(BuildContext context, int index) {
    print(widget.images[index].path);
    return Container(
      // ignore: unrelated_type_equality_checks
      child:
          widget.images[index].type == EnumUtils.enumToString(DataType.IMAGE) &&
                  widget.images[index].path.startsWith("http")
              ? Stack(
                  alignment: Alignment.center,
                  children: [
                    AsperctRaioImage.network(widget.images[index].path,
                        builder: (context, snapshot, url) {
                      double width=snapshot.data.width.toDouble();
                      double height=snapshot.data.height.toDouble();
                      double showWidth=0.0;
                      if(width>height){
                        //宽占满

                      }else{
                       if( (height/width)>4/3){

                       }else{

                       }
                      }

                      return Center(
                        child: Image.network(
                          url,
                          width: snapshot.data.width.toDouble(),
                          height: snapshot.data.height.toDouble(),
                          fit: BoxFit.cover,
                        ),
                      );
                    }),
                    widget.images[index].state == 1
                        ? Image.asset(
                            "picture/icon_under_review.png",
                            package: 'amway_superapp_creator_plugin',
                            width: ScreenUtil().setWidth(80),
                            height: ScreenUtil().setWidth(30),
                          )
                        : widget.images[index].state == 3
                            ? Image.asset(
                                "picture/icon_audit_nopass.png",
                                package: 'amway_superapp_creator_plugin',
                                width: ScreenUtil().setWidth(80),
                                height: ScreenUtil().setWidth(30),
                              )
                            : Text("")
                  ],
                )
              : Image.file(
                  widget.images[index].file == null
                      ? File(widget.images[index].path)
                      : widget.images[index].file,
                  fit: BoxFit.contain,
                ),
    );
  }

  Widget leadingwidget(BuildContext context) {
    return IconButton(
        icon: Icon(Icons.arrow_back_ios),
        color: Colors.white,
        onPressed: () {
          Application.router.pop(context, widget.images);
        });
  }

  Widget delButton(BuildContext context) {
    return InkWell(
      child: Padding(
        padding: EdgeInsets.all(5),
        child: InkWell(
          onTap: () {
            showDialog(
                context: context,
                barrierDismissible: false,
                builder: (_) {
                  return CommTipDialog(
                    content: '确认删除此图片吗',
                    confirmText: "确认",
                    closeText: "取消",
                    confirmCallback: () {
                      if (widget.images[widget.index].state != null &&
                          widget.images[widget.index].state == 1) {
                        ToastUtils.showShortToast("图片正在审核中");
                        return;
                      }
                      widget.images.removeAt(widget.index);
                      widget.index--;
                      setState(() {});
                      if (widget.images.length <= 0) {
                        Application.router.pop(context);
                      }
                    },
                    dismissCallback: () {},
                  );
                });
          },
          child: Image.asset(
            "picture/icon_white_delete.png",
            package: 'amway_superapp_creator_plugin',
          ),
        ),
      ),
    );
  }
}
